package uk.co.wroedevelopments.semanticworkbench;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchListener;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.neo4j.api.core.Node;
import org.neo4j.api.core.Transaction;
import org.osgi.framework.BundleContext;

import uk.co.wroedevelopments.semanticworkbench.store.Connection;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin implements IWorkbenchListener {

	// The plug-in ID
	public static final String PLUGIN_ID = "uk.co.wroedevelopments.semanticworkbench";

	// The shared instance
	private static Activator plugin;
	
	/**
	 * The constructor
	 */
	public Activator() {
	}

	
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		System.out.println("Neo startup");
		try {
		Connection.getDefaultService();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		PlatformUI.getWorkbench().addWorkbenchListener(this);
		
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		System.out.println("Neo shutdown2");
		plugin = null;
		super.stop(context);
		
		
		PlatformUI.getWorkbench().removeWorkbenchListener(this);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	public void postShutdown(IWorkbench arg0) {
		NeoShutDownJob job = new NeoShutDownJob("Shut down Neo");
		job.schedule();
		System.out.println("Neo shutdown3");
	}

	public boolean preShutdown(IWorkbench arg0, boolean arg1) {
		System.out.println("Neo shutdown1");
		return true;
	}
}

class NeoShutDownJob extends Job {
	public NeoShutDownJob(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	public IStatus run(IProgressMonitor monitor) {
		System.out.println("Neo shutdownA1");
		try {
			int count =0;
			Transaction tx = Connection.getDefaultService().beginTx();
			try {
			for(Node n: Connection.getDefaultService().getAllNodes()) {
				count++;
				if (n.hasProperty("name"))
					System.out.println(n.getProperty("name"));
			}
			tx.success();
			System.out.println("number of nodes "+count);
			
			}
			finally {
				tx.finish();
			}
			Connection.getIndexService().shutdown();
			Connection.getDefaultService().shutdown();
		
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("Neo shutdownA2");
		return Status.OK_STATUS;
		
	}
}
 